<?php
/**
 * Created by PhpStorm.
 * User: xiaotao
 * Date: 18-5-3
 * Time: 下午5:20
 *
 */

namespace app\manage\controller;
use app\common\model\Article as Model;
use app\common\validate\Article as Validate;
use app\common\model\Category;
use think\facade\Env;
use think\Request;

class Upload  extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = [
            'items' => [1, 2, 5],
            'total' => 3
        ];
        return $this->result_json(null, 1, $data);
    }

    /**
     * 上传照片接收接口
     *
     */
    public function image(){
        $key = $this->request->param('key', 'article');
        $base_dir = $this->request->root(true) . '/uploads/' . $key . '/';
        $upload_path = $this->app->getRootPath() . 'public/uploads/' . $key;
        // 根据上传的内容，做特殊处理
        switch ($key) {
            case 'avatar':

                break;
            case 'banner':

                break;
            //case 'article': // 不明确的全部当作文章内容附件处理
            default:
                break;
        }
        // 上传文件
        $file = $this->request->file('file');
        $info = $file->move($upload_path);
        if ($info) {
            $data = [
                'base_url' => $base_dir ,
                'save_name'=> $info->getSaveName()
            ];
            return $this->result_json(null, 1, $data);
        } else {
            return $this->result_json($file->getError(), 1001);
        }
    }



}